home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xedit / xedit.c < prev    next >
C/C++ Source or Header  |  1994-09-27  |  5KB  |  174 lines

  1. /* $XConsortium: xedit.c,v 1.27 91/02/17 15:48:25 dave Exp $ */
  2.  
  3. /*
  4.  *              COPYRIGHT 1987
  5.  *           DIGITAL EQUIPMENT CORPORATION
  6.  *               MAYNARD, MASSACHUSETTS
  7.  *            ALL RIGHTS RESERVED.
  8.  *
  9.  * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
  10.  * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
  11.  * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
  12.  * ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
  13.  *
  14.  * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT RIGHTS,
  15.  * APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN ADDITION TO THAT
  16.  * SET FORTH ABOVE.
  17.  *
  18.  *
  19.  * Permission to use, copy, modify, and distribute this software and its
  20.  * documentation for any purpose and without fee is hereby granted, provided
  21.  * that the above copyright notice appear in all copies and that both that
  22.  * copyright notice and this permission notice appear in supporting
  23.  * documentation, and that the name of Digital Equipment Corporation not be 
  24.  * used in advertising or publicity pertaining to distribution of the software
  25.  * without specific, written prior permission.
  26.  */
  27.  
  28. #include "xedit.h"
  29.  
  30. static XtActionsRec actions[] = {
  31. {"quit", DoQuit}
  32. };
  33.  
  34. static Atom wm_delete_window;
  35.  
  36. Widget textwindow, messwidget, labelwindow, filenamewindow;
  37.  
  38. void ResetSourceChanged();
  39.  
  40. static void makeButtonsAndBoxes();
  41.  
  42. Display *CurDpy;
  43.  
  44. struct _app_resources app_resources;
  45.  
  46. #define Offset(field) XtOffsetOf(struct _app_resources, field)
  47.  
  48. static XtResource resources[] = {
  49.    {"enableBackups", "EnableBackups", XtRBoolean, sizeof(Boolean),
  50.          Offset(enableBackups), XtRImmediate, FALSE},
  51.    {"backupNamePrefix", "BackupNamePrefix", XtRString, sizeof(char *),
  52.          Offset(backupNamePrefix),XtRString, ""},
  53.    {"backupNameSuffix", "BackupNameSuffix", XtRString, sizeof(char *),
  54.          Offset(backupNameSuffix),XtRString, ".BAK"}
  55. };
  56.  
  57. #undef Offset
  58.  
  59. void
  60. main(argc, argv)
  61. int argc;
  62. char **argv;
  63. {
  64.   XtAppContext appcon;
  65.   Widget top;
  66.   String filename = NULL;
  67.  
  68.   top = XtAppInitialize(&appcon, "Xedit", NULL, 0, &argc, argv, NULL, NULL, 0);
  69.  
  70.   XtAppAddActions(appcon, actions, XtNumber(actions));
  71.   XtOverrideTranslations
  72.       (top, XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));
  73.   
  74.   XtGetApplicationResources(top, (XtPointer) &app_resources, resources,
  75.                 XtNumber(resources), NULL, 0);
  76.  
  77.   CurDpy = XtDisplay(top);
  78.  
  79.   if (argc > 1) {
  80.     Boolean exists;
  81.     filename = argv[1];
  82.  
  83.     switch ( CheckFilePermissions(filename, &exists)) {
  84.     case NO_READ:
  85.     if (exists)
  86.         fprintf(stderr, 
  87.             "File %s exists, and could not opened for reading.\n", 
  88.             filename);
  89.     else
  90.         fprintf(stderr, "File %s %s %s",  filename, "does not exist,",
  91.             "and the directory could not be opened for writing.\n");
  92.     exit(1);
  93.     case READ_OK:
  94.     case WRITE_OK:
  95.     makeButtonsAndBoxes(top, filename);
  96.     break;
  97.     default:
  98.     fprintf(stderr, "%s %s", "Internal function MaybeCreateFile()",
  99.         "returned unexpected value.\n");
  100.     exit(1);
  101.     }
  102.   }  
  103.   else
  104.       makeButtonsAndBoxes(top, NULL);
  105.  
  106.   XtRealizeWidget(top);
  107.   XDefineCursor(XtDisplay(top),XtWindow(top),
  108.         XCreateFontCursor( XtDisplay(top), XC_left_ptr));
  109.   
  110.   wm_delete_window = XInternAtom(XtDisplay(top), "WM_DELETE_WINDOW",
  111.                  False);
  112.   (void) XSetWMProtocols (XtDisplay(top), XtWindow(top),
  113.               &wm_delete_window, 1);
  114.   
  115.   XtAppMainLoop(appcon);
  116. }
  117.  
  118. static void
  119. makeButtonsAndBoxes(parent, filename)
  120. Widget parent;
  121. char * filename;
  122. {
  123.   Widget outer, b_row;
  124.   Arg arglist[10];
  125.   Cardinal num_args;
  126.  
  127.   outer = XtCreateManagedWidget( "paned", panedWidgetClass, parent,
  128.                 NULL, ZERO);
  129.  
  130.   b_row= XtCreateManagedWidget("buttons", panedWidgetClass, outer, NULL, ZERO);
  131.   {
  132.     MakeCommandButton(b_row, "quit", DoQuit);
  133.     MakeCommandButton(b_row, "save", DoSave);
  134.     MakeCommandButton(b_row, "load", DoLoad);
  135.     filenamewindow = MakeStringBox(b_row, "filename", filename); 
  136.   }
  137.   XtCreateManagedWidget("bc_label", labelWidgetClass, outer, NULL, ZERO);
  138.  
  139.   num_args = 0;
  140.   XtSetArg(arglist[num_args], XtNeditType, XawtextEdit); num_args++;
  141.   messwidget = XtCreateManagedWidget("messageWindow", asciiTextWidgetClass,
  142.                       outer, arglist, num_args);
  143.  
  144.   num_args = 0;
  145.   if (filename != NULL) 
  146.     XtSetArg(arglist[num_args], XtNlabel, filename); num_args++;
  147.  
  148.   labelwindow = XtCreateManagedWidget("labelWindow",labelWidgetClass, 
  149.                       outer, arglist, num_args);
  150.  
  151.   num_args = 0;
  152.   XtSetArg(arglist[num_args], XtNtype, XawAsciiFile); num_args++;
  153.   XtSetArg(arglist[num_args], XtNeditType, XawtextEdit); num_args++;
  154.   textwindow =  XtCreateManagedWidget("editWindow", asciiTextWidgetClass, 
  155.                       outer, arglist, num_args);
  156.  
  157.   if (filename != NULL)
  158.       DoLoad();
  159.   else
  160.       ResetSourceChanged(textwindow);
  161. }
  162.  
  163. /*    Function Name: Feep
  164.  *    Description: feeps the bell.
  165.  *    Arguments: none.
  166.  *    Returns: none.
  167.  */
  168.  
  169. void
  170. Feep()
  171. {
  172.   XBell(CurDpy, 0);
  173. }
  174.